/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Pe.edu.unmsm.SIASS.DAOImpl;

import Pe.edu.unmsm.SIASS.DAO.SesionDAO;
import Pe.edu.unmsm.SIASS.Dominio.Capitulo;
import Pe.edu.unmsm.SIASS.Dominio.Sesion;
import Util.HibernateUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Fabrica
 */
public class SesionDAOImpl implements SesionDAO, Serializable {

    @Override
    public List<Sesion> listaSesionCap(int idCapitulo) {
        List<Sesion> lista = new ArrayList<Sesion>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            Query query = session.createQuery("select s from Sesion s, Capitulo c where s.capitulo=c and c.idCaptiulo=" + idCapitulo);
            lista = (List<Sesion>) query.list();
            for (Sesion ses : lista) {
                Hibernate.initialize(ses.getCapitulo());
                Hibernate.initialize(ses.getCapitulo().getCursoPeriodo());
                Hibernate.initialize(ses.getCapitulo().getCursoPeriodo().getCurso());
                Hibernate.initialize(ses.getCapitulo().getCursoPeriodo().getPeriodo());
            }
            session.beginTransaction().commit();
            session.close();
        } catch (Exception e) {
            session.beginTransaction().rollback();
            e.printStackTrace();
        }
        return lista;
    }
}
